package preorder
import (
    //"time"
	"strings"
	"errors"
	"encoding/json"
	"dhfshop/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type TemplateTmp struct {
	Periods []*PeriodStl `json:"periods"`
}

type Template struct {
	TemplateId    int64       `orm:"column(template_id);pk;auto" json:"template_id" form:"template_id"`
    PartnerId     int64       `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`    
    StoreId       int64       `orm:"column(store_id)" json:"store_id" form:"store_id"`
	Title         string      `orm:"column(title)" json:"title" form:"title"`                   //标题
	IsBuiltIn     int32       `orm:"column(is_built_in)" json:"is_built_in" form:"is_built_in"` //是否内建:1内建，0为非      
	TypeId        int         `orm:"column(type_id)" json:"type_id" form:"type_id"` //类型1:以周为编排单元2:以天为编排单元
	ExcludeByWeek string      `orm:"column(exclude_by_week)" json:"exclude_by_week" form:"exclude_by_week"` //按星期几避开
	ExcludeByDate string      `orm:"column(exclude_by_date)" json:"exclude_by_date" form:"exclude_by_date"` //按日期避开
	PeriodsJson   string      `orm:"column(periods);type(text)" json:"periods" form:"periods"`
}

type TemplateHub struct {
	PartnerId     int64      `json:"partner_id"` 
	StoreId       int64      `json:"store_id"`
	TemplateId    int64      `json:"template_id"`
	TypeId        int        `json:"type_id"`         //类型1:以周为编排单元2:以天为编排单元
	ExcludeByWeek string     `json:"exclude_by_week"` //按星期几避开
	ExcludeByDate string     `json:"exclude_by_date"` //按日期避开
	PeriodMaps  map[string]*PeriodStl `json:"period_maps"`
}

type PeriodStl struct {
	PeriodNo    int            `json:"period_no"`           //周期序号
	Name        string         `json:"name"`                //名称,比如:周一、周二...
	SourceAtStr string         `json:"source_at,omitempty"` //号源时间,只要日期到天
	Timeslots   []*TimeslotStl `json:"timeslots"`
}

type TimeslotStl struct {
	TimeslotNo    int    `json:"timeslot_no"`              //时段序号
	TimeslotLabel string `json:"timeslot_label,omitempty"` //时段标签
	Start         string `json:"start"`                    //开始时间,非全日期时间,比如: 11:00
	End           string `json:"end"`                      //结束时间,非全日期时间,比如: 12:00
	CurCount      int    `json:"cur_count,omitempty"`      //当前预约量
	MaxCount      int    `json:"max_count"`                //最大可预约量
}

//系统周期公有模板
func GetWeekTemplate() (template *TemplateTmp) {
	template = new(TemplateTmp)
	/**********周一**********/
	period := &PeriodStl{
		PeriodNo: 1,
		Name:     "周一",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	/**********周二**********/
	period = &PeriodStl{
		PeriodNo: 2,
		Name:     "周二",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	/**********周三**********/
	period = &PeriodStl{
		PeriodNo: 3,
		Name:     "周三",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	/**********周四**********/
	period = &PeriodStl{
		PeriodNo: 4,
		Name:     "周四",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	/**********周五**********/
	period = &PeriodStl{
		PeriodNo: 5,
		Name:     "周五",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	/**********周六**********/
	period = &PeriodStl{
		PeriodNo: 6,
		Name:     "周六",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	/**********周日**********/
	period = &PeriodStl{
		PeriodNo: 7,
		Name:     "周日",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	return template
}

//天周期公有模板
func GetDayTemplate() (template *TemplateTmp) {
	template = new(TemplateTmp)
	/**********每天**********/
	period := &PeriodStl{
		PeriodNo: 1,
		Name:     "每天",
	}
	//时段1
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 1,
		Start:      "09:00",
		End:        "10:00",
		MaxCount:   3,
	})
	//时段2
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 2,
		Start:      "10:00",
		End:        "11:00",
		MaxCount:   3,
	})
	//时段3
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 3,
		Start:      "11:00",
		End:        "12:00",
		MaxCount:   3,
	})
	//时段4
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 4,
		Start:      "13:00",
		End:        "14:00",
		MaxCount:   3,
	})
	//时段5
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 5,
		Start:      "14:00",
		End:        "15:00",
		MaxCount:   3,
	})
	//时段6
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 6,
		Start:      "15:00",
		End:        "16:00",
		MaxCount:   3,
	})
	//时段7
	period.Timeslots = append(period.Timeslots, &TimeslotStl{
		TimeslotNo: 7,
		Start:      "16:00",
		End:        "17:00",
		MaxCount:   3,
	})
	template.Periods = append(template.Periods, period)
	return template
}

func InitPubTemplate()(err error) {
    if _, err = GetTemplateByPartnerId(0, 0); err == orm.ErrNoRows {
		template := new(Template)
		template.PartnerId = 0
		template.StoreId = 0
		template.Title = "周模板"
		template.IsBuiltIn = 1
		template.TypeId = 1
		template.ExcludeByWeek = ""
		template.ExcludeByDate = ""
		weekTmp := GetWeekTemplate()
		if weekTmpJson, err := json.Marshal(weekTmp); err == nil {
		   template.PeriodsJson = string(weekTmpJson)
		}
		if _, err = AddTemplate(template); err != nil {
		   beego.Debug(err.Error())
		}
		//
		template = new(Template)
		template.PartnerId = 0
		template.StoreId = 0
		template.Title = "天模板"
		template.IsBuiltIn = 1
		template.TypeId = 2
		template.ExcludeByWeek = ""
		template.ExcludeByDate = ""
		dayTmp := GetDayTemplate()
		if dayTmpJson, err := json.Marshal(dayTmp); err == nil {
		   template.PeriodsJson = string(dayTmpJson)
		}
		if _, err = AddTemplate(template); err != nil {
		    beego.Debug(err.Error())
		}
	}
	return
}


func init() {
	orm.RegisterModel(new(Template))
}


func AddTemplate(m *Template) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}


func GetTemplateById(id int64) (v *Template, err error) {
	o := models.GetDefaultDB()
	v = &Template{TemplateId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetTemplateByPartnerId(partnerId, storeId int64) (v *Template, err error) {
	o := models.GetDefaultDB()
	v = &Template{PartnerId:partnerId, StoreId:storeId}
	if err = o.Read(v, "PartnerId", "StoreId"); err == nil {
		return v, nil
	}
	return nil, err
}

func GetAllTemplates(query map[string]string,fields []string,order_by map[string]string,offset int,limit int)([]*Template, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(Template))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	cnt, err := qs.Count()
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	var lists []*Template
	qs = qs.OrderBy(sortFields...)
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		return lists, cnt, nil
	}
	return nil, 0, err
}
func UpdateTemplateById(m *Template, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := Template{TemplateId: m.TemplateId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}
func DeleteTemplate(id int64) (err error) {
	o := models.GetDefaultDB()
	v := Template{TemplateId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

